import { getFirstMenuPath, filterRouters } from '@/unit/tool';

// function getModuleId(lists, activeMenuPath, id = '') {
//   for (let index in lists) {
//     if (lists[index].path == activeMenuPath) {
//       id = lists[index].parentId;
//       // break;
//     }
//     if (lists[index].children && lists[index].children.length > 0) {
//       id = getModuleId(lists[index].children, activeMenuPath, id);
//     }
//   }
//   return id;
// }

function findTreePathLists(lists, activeMenuPath) {
  if (!(lists instanceof Array)) {
    console.error('need lists');
    return [];
  }
  if (lists.length == 0) {
    return [];
  }

  for (let i = 0; i < lists.length; i++) {
    const item = lists[i];
    if (item['path'] == activeMenuPath) {
      // 如果找到,则跳出
      return [item['id']];
    }

    if (item.children && item.children.length) {
      const path = findTreePathLists(item.children, activeMenuPath);
      if (path.length > 0) {
        // 字节点找到,把本节点插入到第一个
        path.splice(0, 0, item['id']);
        return path;
      }
    }
  }
  return []
}

// 获取某个页面下的子页面或者按钮
function getBtnListsOrPageLists(menuLists, pagePath, type) {
  let lists = getLists(menuLists, pagePath);
  let BtnListsOrPageLists = getItem(lists, type);
  return BtnListsOrPageLists;

  function getLists(lists, pagePath, arr = []) {
    for (let index in lists) {
      if (lists[index].path == pagePath) {
        arr = lists[index].children;
        break;
      }
      if (lists[index].children && lists[index].children.length > 0) {
        arr = getLists(lists[index].children, pagePath, arr);
      }
    }
    return arr;
  }

  function getItem(lists, type, arr = []) {
    for (let index in lists) {
      if (lists[index].type == type) {
        arr.push(lists[index]);
        // break;
      }
      if (lists[index].children && lists[index].children.length > 0) {
        arr = getItem(lists[index].children, type, arr);
      }
    }
    return arr;
  }
}

function getMenuModule(lists) {
  return lists.map((item) => {
    let { children, ...rest } = item;
    let firstMenuPath = children[0].path;
    return {
      ...rest,
      firstMenuPath,
    };
  });
}

export default {
  name: 'menu',
  namespaced: true,
  state: {
    menuLists: [], //当前app菜单
    menuModuleLists: [], //当前app所有的模块
    moduleId: '', //当前模块id
    // currentModuleMenus: [], //当前模块下所有的菜单
    allAuthRoutes: [], //所有授权的路由表
    authBtnlists: [], //获取当前页面的按钮权限
    authPagelists: [], //获取当前页面的授权子页面
    firstMenuPath: '', //首菜单项路径
    activeMenuPath: '', //当前被激活的菜单
  },
  mutations: {
    // 获取当前app菜单
    saveMenuLists(state, payload) {
      state.menuLists = [...payload];
      let lists = filterRouters(payload, 3);
      lists.push({ path: '/' });
      lists.push({ path: '/index' });
      // state.allAuthRoutes = filterRouters(payload, 3);
      state.allAuthRoutes = lists;

      state.firstMenuPath = `${getFirstMenuPath(payload)}`;

      state.menuModuleLists = getMenuModule(payload);
    },
    setActiveMenuPath(state, payload) {
      let { path } = payload;
      state.activeMenuPath = path;
      // state.moduleId = getModuleId(state.menuLists, path.split('/')[1]);
      state.moduleId = findTreePathLists(state.menuLists, path)[0];
    },
    getAuthBtnlists(state, payload) {
      let { path } = payload;
      state.authBtnlists = getBtnListsOrPageLists(state.menuLists, path, 4).map((item) => item.path);
      state.authPagelists = getBtnListsOrPageLists(state.menuLists, path, 3);
    },
  },
};
